package com.mrd.gtimp.base.entity;

import org.hibernate.annotations.Formula;
import org.hibernate.annotations.GenericGenerator;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

/**
 * <p> application name: sample <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * 模块对象
 *
 * @author qiul
 */
@Entity(name = "t_sys_module")
public class Module implements Comparable<Module>, Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(generator = "moduleGenerator")
    @GenericGenerator(name = "moduleGenerator", strategy = "uuid")
    @Column(name = "id")
    private String id;
    /**
     * 栏目名称
     */
    @Column(name = "module_name")
    private String name;
    /**
     * 对应url
     */
    @Column(name = "link")
    private String link;
    /**
     * 启用状态
     */
    @Column(name = "m_state")
    private String state = "0";
    /**
     * 父栏目
     */
    @ManyToOne
    @JoinColumn(name = "pid", referencedColumnName = "id")
    private Module parent;
    /**
     * 展示顺序
     */
    @Column(name = "m_order")
    private Integer order = 99;

    /**
     * 临时字段，是否为父对象
     */
    @Formula(
            value = "case when pid is null then 1 when (select count(*) from t_sys_module m where m.pid=id) > 0 then 1 else 0 end")
    private boolean isParent;

    /**
     * 临时字段，在ztree中checked标记
     */
    @Transient
    private boolean checked;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLink() {
        return link;
    }

    public void setLink(String link) {
        this.link = link;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Module getParent() {
        return parent;
    }

    public void setParent(Module parent) {
        this.parent = parent;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    public boolean getIsParent() {
        return isParent;
    }

    public void setIsParent(boolean isParent) {
        this.isParent = isParent;
    }

    public boolean getChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        Module other = (Module) obj;
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        return true;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    @Override
    public int compareTo(Module o) {
        int i = 0;
        if (o == null) {
            i = 1;
        } else if (o.order == null) {
            i = 1;
        } else if (this.order == null) {
            i = -1;
        } else {
            i = this.order.compareTo(o.order);
        }
        if (i == 0) {
            if (o.name == null) {
                i = 1;
            } else if (this.name == null) {
                i = -1;
            } else {
                i = this.name.compareTo(o.name);
            }
        }
        return i;
    }
}
